package com.qimei.onerice.servlet;

import com.google.gson.Gson;
import com.qimei.onerice.entity.Admin;
import com.qimei.onerice.entity.Customer;
import com.qimei.onerice.service.AdminService;
import com.qimei.onerice.service.CustomerService;
import com.qimei.onerice.service.impl.AdminServiceImpl;
import com.qimei.onerice.service.impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/customer")
public class CustomerServlet extends HttpServlet {
	CustomerService customerService = new CustomerServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("UTF-8");

		String action = request.getParameter("action");

		if ("getAll".equals(action)) {
			// 获取所有客户数据
			handleGetList(request, response);
		} else if ("getById".equals(action)) {
			// 获取单个客户数据
			handleGetById(request, response);
		} else {
			// 未知的操作类型
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().println("未知的操作");
		}
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");

		System.out.println(action);
		if ("add".equals(action)) {
			// 添加客户
			handleAdd(request, response);
		} else if ("update".equals(action)) {
			// 更新客户
			handleUpdate(request, response);
		} else if ("delete".equals(action)) {
			// 删除客户
			handleDelete(request, response);
		} else {
			// 未知的操作类型
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().println("未知操作");
		}
	}
	// 添加新客户
	private void handleAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String customerName = request.getParameter("customerName");
		String customerUserName = request.getParameter("customerUsername");
		String customerEmail = request.getParameter("customerEmail");
		String customerPhone = request.getParameter("customerPhone");

		// 创建一个 Customer 对象
		Customer newCustomer = new Customer();
		newCustomer.setCustomerName(customerName);
		newCustomer.setCustomerPhone(customerPhone);
		newCustomer.setCustomerUsername(customerUserName);
		newCustomer.setCustomerEmail(customerEmail);
		newCustomer.setCustomerImage("user.png");

		// 调用服务方法添加新客户
		boolean success = customerService.addCustomer(newCustomer);

		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 更新客户
	private void handleUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// 从请求中提取参数
		String customerIdParam = request.getParameter("customerId");
		int customerId = Integer.parseInt(customerIdParam);
		String customerName = request.getParameter("name");
		String customerUserName = request.getParameter("username");
		String customerEmail = request.getParameter("email");
		String customerPhone = request.getParameter("contact");

		Customer updatedCustomer = new CustomerServiceImpl().getCustomerById(customerId);
		updatedCustomer.setCustomerId(customerId);
		updatedCustomer.setCustomerName(customerName);
		updatedCustomer.setCustomerUsername(customerUserName);
		updatedCustomer.setCustomerEmail(customerEmail);
		updatedCustomer.setCustomerPhone(customerPhone);

		// 调用服务方法更新客户
		customerService.updateCustomer(updatedCustomer);
		request.getSession().setAttribute("user", updatedCustomer);
		// 发送响应
		request.getRequestDispatcher("/view/customerInfo.jsp").forward(request, response);
	}

	// 删除客户
	private void handleDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String customerId = request.getParameter("customerId");

		// 调用服务方法删除客户
		boolean success = customerService.deleteCustomer(Integer.parseInt(customerId));

		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 根据ID查询客户
	private void handleGetById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String customerIdParam = request.getParameter("customerId");

		int customerId = Integer.parseInt(customerIdParam);

		// 调用方法根据ID获取客户
		Customer customer = customerService.getCustomerById(customerId);

		// 将 Customer 对象转换为 JSON 并发送响应
		String jsonData = new Gson().toJson(customer);
		sendJsonResponse(response, jsonData);
	}


	private void handleGetList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Customer> customerList = customerService.getCustomerList();
		// 将 Customer 对象转换为 JSON 并发送响应
		String jsonData = new Gson().toJson(customerList);
		sendJsonResponse(response, jsonData);
	}

	private void sendJsonResponse(HttpServletResponse response, String jsonData) throws IOException {
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(jsonData);
		out.flush();
	}
}
